﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CommandLineHandlerExtensions.cs" company="Matthias Friedrich">
//   Copyright © Matthias Friedrich 2013
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace CommandlineHandling
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public static class CommandLineHandlerExtensions
    {
        public static IEnumerable<ICommandLineOption> FindByName(this IEnumerable<ICommandLineOption> options, string name)
        {
            ExceptionExtensions.ThrowArgumentNullException(options, () => options);

            return options.Where((x) => x.Name == name);
        }

        public static IEnumerable<OptionParameter> FindParametersName(this IEnumerable<ICommandLineOption> options, string name)
        {
            ExceptionExtensions.ThrowArgumentNullException(options, () => options);

            return from option in options
                from parameter in option.Parameters
                where parameter.Name == name
                select parameter;
        }

        public static IEnumerable<TValue> Select<TValue>(this IEnumerable<OptionParameter> parameters, Func<string, TValue> valueConverter)
        {
            ExceptionExtensions.ThrowArgumentNullException(parameters, () => parameters);

            return parameters.Select<OptionParameter, TValue>((x) => valueConverter(x.Value));
        }
    }
}